package org.example.taobao.demo.good.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.taobao.demo.good.repository.entity.GoodDO;
import org.example.taobao.demo.good.service.GoodService;
import org.example.taobao.demo.good.vo.DeductStockVO;
import org.example.taobao.demo.good.vo.GoodVO;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;


/**
 * @author zhangxx
 * @date 2022/6/9 8:15 PM
 */
@Api(tags="产品服务")
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/good")
public class GoodController {
    private final GoodService goodService;

    @PostMapping
    public String addGood(@RequestBody GoodVO goodVO) {
        Assert.notNull(goodVO, "参数不能为空");
        return goodService.addGood(GoodConvert.toDO(goodVO));
    }

    @GetMapping("{goodId}")
    public GoodVO queryGood(@PathVariable("goodId") String goodId) {
        log.info("queryGood:"+goodId);
        GoodDO goodDO = goodService.queryGood(goodId)
                .orElseThrow(() -> new IllegalArgumentException("产品["+goodId+"]不存在"));
        return GoodConvert.toVO(goodDO);
    }

    @PostMapping("deduct")
    public boolean deductStock(@RequestBody DeductStockVO deductStockVO) {
        return goodService.deductStock(deductStockVO.getGoodId(), deductStockVO.getNumber());
    }
}
